﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace PartyCraftServer
{
    public class Analytics
    {
        public static Queue<float> CpuUsage; // TODO: Circular array
        public static Queue<int> ConnectedPlayers;

        public static void Initialize()
        {
            CpuUsage = new Queue<float>();
            ConnectedPlayers = new Queue<int>();
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            LastPlayerSnapshot = DateTime.Now;
            LastPlayerSnapshot = new DateTime(LastPlayerSnapshot.Year, LastPlayerSnapshot.Month, LastPlayerSnapshot.Day,
                LastPlayerSnapshot.Hour, (int)(((double)LastPlayerSnapshot.Minute / 60) * 4), 0);
        }

        public static void TakeSnapshot()
        {
            CpuUsage.Enqueue(cpuCounter.NextValue());
            if (CpuUsage.Count > 50)
                CpuUsage.Dequeue();
            if ((DateTime.Now - LastPlayerSnapshot).TotalMinutes > 15)
            {
                ConnectedPlayers.Enqueue(PartyCraft.Server.GetLoggedInClients().Count());
                if (ConnectedPlayers.Count > 50)
                    ConnectedPlayers.Dequeue();
                LastPlayerSnapshot = DateTime.Now;
                LastPlayerSnapshot = new DateTime(LastPlayerSnapshot.Year, LastPlayerSnapshot.Month, LastPlayerSnapshot.Day,
                    LastPlayerSnapshot.Hour, (int)(((double)LastPlayerSnapshot.Minute / 60) * 4), 0);
            }
        }

        static PerformanceCounter cpuCounter;
        static DateTime LastPlayerSnapshot;
    }
}
